package com.lanou3g.zjl.chating.home.friends;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by dllo on 15/11/20.
 */
public class AddFriendsActivity extends BaseActivity {
    @Bind(R.id.search_contacts)
    EditText writeUserName;//查找的用户名
    @Bind(R.id.search_contacts_reason)
    EditText reason;//原因
    @Bind(R.id.search_contacts_name)
    TextView name; //找到的用户名
    @Bind(R.id.showAddWho)
    RelativeLayout showAddWho;

    @Override
    protected int getLayout() {

        return R.layout.add_contacts;
    }

    @Override
    protected void initData() {
        showAddWho.setVisibility(View.GONE);
    }
    //查找好友点击事件
    @OnClick(R.id.search_contacts_btn) void search(){
        if(TextUtils.isEmpty(writeUserName.getText().toString())){

            Toast.makeText(AddFriendsActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
        }else {
            name.setText(writeUserName.getText());
            showAddWho.setVisibility(View.VISIBLE);
        }


    }
    @OnClick(R.id.finishAddFriend) void finishAddFriend(){
        finish();
    }
    //添加好友点击事件
    @OnClick(R.id.search_contacts_add) void add(){

        //输入的是自己用户名则不能添加
        if(EMChatManager.getInstance().getCurrentUser().equals(name.getText().toString())){
            Toast.makeText(this,"不能添加自己为好友",Toast.LENGTH_SHORT).show();
            return;
        }
        List<String> newFriends = new ArrayList<>();
        EMChatManager.getInstance().getChatOptions().setUseRoster(true);
        try {
            newFriends = EMContactManager.getInstance().getContactUserNames();
        } catch (EaseMobException e) {
            e.printStackTrace();
        }

        if(newFriends.contains(name.getText().toString())){
            //提示已在好友列表中(在黑名单列表里)，无需添加
            if(EMContactManager.getInstance().getBlackListUsernames().contains(name.getText().toString())){
                showAddWho.setVisibility(View.GONE);
                writeUserName.setText("");
                reason.setText("");
                Toast.makeText(this,"该用户已在你的黑名单中",Toast.LENGTH_SHORT).show();
                return;
            }
            showAddWho.setVisibility(View.GONE);
            writeUserName.setText("");
            reason.setText("");
            Toast.makeText(this,"该好友已存在",Toast.LENGTH_SHORT).show();
            return;
        }
        new Thread(new Runnable() {
            public void run() {

                try {
                    EMContactManager.getInstance().addContact(name.getText().toString(), reason.getText().toString());

                    runOnUiThread(new Runnable() {
                        public void run() {

                            Toast.makeText(getApplicationContext(), "请求发送成功,等待验证!!", Toast.LENGTH_LONG).show();
                            showAddWho.setVisibility(View.GONE);
                            writeUserName.setText("");
                            reason.setText("");
                        }
                    });
                } catch (final Exception e) {

                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(getApplicationContext(), "请求发送失败" + e.getMessage(), Toast.LENGTH_SHORT).show();
                            showAddWho.setVisibility(View.GONE);
                            writeUserName.setText("");
                            reason.setText("");
                        }
                    });
                }
            }
        }).start();

    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
